import * as lib from '../../../components/aotoo/lib/index'
const dateInfoHooks = lib.hooks('date-info', true)

// 加班
export default function({tapDay, today, funButtons, record, caseKey}){
  const that = this
  const key = caseKey

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key
    }
    return item
  })

  // funButtons.dot.shift()

  const modalData = {
    total: 0,
    slider: 0,
  }

  const alertsData = dateInfoHooks.getItem('case-alerts')
  const overjob = alertsData.overjob

  if (overjob) {
    overjob.forEach(item=>{
      modalData.total += (item.slider||0)
    })
  }

  if (record) {
    modalData.slider = record.slider||0
  }

  const title = `${tapDay.month}月${tapDay.day}日（${tapDay.lunarDate.ncWeek}）`
  const content = {body: [title], date: tapDay.timestr, itemClass: 'overjob', ...funButtons}

  function beforeCallback({createCaseCard}){
    let additionalJobTime = 0
    wx.modalx.showModal({
      content: {
        title: [
          '累计加班', 
          {$$id: 'addJobTime', title: `${modalData.total}`, itemClass: 'inner-content-case-title-count'}, 
        ], 
        body: [
          // `薪水日为每月 ${today.day} 号`, 
          `当前日期：${tapDay.timestr}${tapDay.timestr === today.timestr ? '(今天)':''}`, 
          {
            itemClass: 'inner-content-case-body',
            title: '加班时长:',
            "@slider": {
              min: 0,
              max: 20,
              value: modalData.slider,
              // itemStyle: 'width: 62%;',
              "block-size": 30,
              "show-value": true,
              "block-color": 'rgb(197, 197, 197, 0.3)',
              bindchange: 'changeTimmer'
            },
          },

          // '帮助：在日历中选中日期，添加加班记录',

          {
            title: '我的加班记录', 
            itemClass: 'pop-case-scroll-bottom', 
            aim(){
              wx.modalx.closeModal()
              that.restoreCaseButton()
              wx.pageScrollTo({
                selector: '#pits-list-container',
                duration: 300,
                success(){
                  that.listCaseType('overjob')
                }
              })
            }}, 
        ],
        methods: {
          changeTimmer(e, param, inst){
            additionalJobTime = e.detail.value
            modalData.total += (additionalJobTime - modalData.slider)
            modalData.slider = additionalJobTime
            content.body[1] = '加班'+additionalJobTime+'小时'

            const showZone = this.activePage.getElementsById('addJobTime')
            showZone && showZone .update({
              title: `${modalData.total}`
            })
          }
        },
        itemClass: 'inner-content',
      },
      confirmText: '确定',
      cancelText: '取消', 
      height: 60,
      width: 80,
      itemClass: 'popup overjob', 
      mask: false,
      success(res){
        if (res.confirm) {
          if (tapDay.timestamp > today.timestamp) {
            wx.showToast({
              title: '不能大于今天',
              icon: 'error',
              duration: 2000
            })
            return
          }
          createCaseCard({...content, ...modalData})
        }
        that.restoreCaseButton()
      }
    })
  }
  
  return {key, content, beforeCallback}
}